package util

import (
	"htgis/monitor/module/agent/model"

	"github.com/shirou/gopsutil/mem"
)

const (
	// BYTE unit
	BYTE = 1.0
	// KB unit
	KB = 1024 * BYTE
	// MB unit
	MB = 1024 * KB
	// GB unit
	GB = 1024 * MB
)

// MemStat does statistics of memory
func MemStat() (model.Memory, error) {
	v, err := mem.VirtualMemory()
	if err != nil {
		return model.Memory{}, err
	}
	memStat := model.Memory{
		Total:     RoundFormat(float64(v.Total)/GB, 1),
		Available: RoundFormat(float64(v.Available)/GB, 1),
		Used:      RoundFormat(float64(v.Used)/GB, 1),
	}
	memStat.UsedPercent = RoundFormat(memStat.Used/memStat.Total, 2) * 100
	return memStat, nil
}
